package mn.slide.petrovis.contact;

import mn.slide.petrovis.BranchesActivity;
import mn.slide.petrovis.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ContactActivity extends Activity implements OnClickListener
{
	private TextView	p1 , p2 , p3 , p4 , p5 , p6 , p7 , p8 , p9 , p10;
	private Button		suggestion_btn;

	@Override
	protected void onCreate( Bundle savedInstanceState )
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_activity);
		init();
	}

	private void init()
	{
		p1 = (TextView) findViewById(R.id.p1);
		p1.setOnClickListener(this);
		p2 = (TextView) findViewById(R.id.p2);
		p2.setOnClickListener(this);
		p3 = (TextView) findViewById(R.id.p3);
		p3.setOnClickListener(this);
		p4 = (TextView) findViewById(R.id.p4);
		p4.setOnClickListener(this);
		p5 = (TextView) findViewById(R.id.p5);
		p5.setOnClickListener(this);
		p6 = (TextView) findViewById(R.id.p6);
		p6.setOnClickListener(this);
		p7 = (TextView) findViewById(R.id.p7);
		p7.setOnClickListener(this);
		p8 = (TextView) findViewById(R.id.p8);
		p8.setOnClickListener(this);
		p9 = (TextView) findViewById(R.id.p9);
		p9.setOnClickListener(this);
		p10 = (TextView) findViewById(R.id.p10);
		p10.setOnClickListener(this);
		suggestion_btn = (Button) findViewById(R.id.suggestion_btn);
		suggestion_btn.setOnClickListener(this);
	}

	@Override
	public void onClick( View v )
	{
		int i = v.getId();
		String phoneNumber = "";
		switch (i)
		{
			case R.id.p1:
				phoneNumber = "976-11-327051";
				break;
			case R.id.p2:
				sendMail("info@petrovis.mn");
				break;
			case R.id.p3:
				phoneNumber = "976-11-330909";
				break;
			case R.id.p4:
				phoneNumber = "976-11-330153";
				break;
			case R.id.p5:
				phoneNumber = "976-70110808";
				break;
			case R.id.p6:
				phoneNumber = "976-11-329001";
				break;
			case R.id.p7:
				phoneNumber = "976-11-311583";
				break;
			case R.id.p8:
				phoneNumber = "976-11-330606";
				break;
			case R.id.p9:
				phoneNumber = "976-11-319801";
				break;
			case R.id.p10:
				phoneNumber = "976-11-330707";
				break;
			case R.id.suggestion_btn:
				Intent intent = new Intent(ContactActivity.this, BranchesActivity.class);
				ContactTabGroupActivity.group.goIntent(intent);
				break;
			default:
				break;
		}
		if (!phoneNumber.equals(""))
			callNumber(phoneNumber);

	}

	private void callNumber( final String title )
	{
		AlertDialog.Builder dialog = new AlertDialog.Builder(getParent());
		dialog.setTitle(title);
		dialog.setNegativeButton("Болих", new DialogInterface.OnClickListener()
		{

			@Override
			public void onClick( DialogInterface dialog , int which )
			{
				dialog.dismiss();
			}

		});
		dialog.setPositiveButton("Залгах", new DialogInterface.OnClickListener()
		{

			@Override
			public void onClick( DialogInterface dialog , int which )
			{
				try
				{
					Intent intent = new Intent(Intent.ACTION_CALL);
					intent.setData(Uri.parse("tel:" + title));
					startActivity(intent);
				}
				catch (Exception e)
				{
					Log.e("SampleApp", "Failed to invoke call", e);
				}
			}

		});
		dialog.show();
	}

	private void sendMail( String mail )
	{
		Intent intent = new Intent(android.content.Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { mail });
		intent.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
		intent.putExtra(Intent.EXTRA_TEXT, "Sent from my Android");
		startActivity(Intent.createChooser(intent, "Send mail"));
	}
}
